package com.hb.core.config;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @description: swagger 配置类
 * @projectName: zhb-frame-build
 * @author: zhouhaizhi
 * @createTime: 2021/9/1 14:03
 * @version:1.0
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        parameterBuilder.name("Authorization")
                .description("令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(true)
                .defaultValue("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiemhiLWZyYW1lLWJ1aWxkIl0sInNjb3BlIjpbIndyaXRlIiwicmVhZCJdLCJleHAiOjE2MzMwNzc5MjgsImp0aSI6ImViNTM2ZGQxLWJlNzctNDk0OC05ZGU0LWVmNjM2OWZhN2JmNyIsImNsaWVudF9pZCI6ImFkbWluIn0.P2ENGxi5b8dVj6XKgV1prp0Lqjm5P4342H8Em8hLSTw");
        List<Parameter> parameters = CollectionUtil.newArrayList(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //当前包路径，配置扫描路径
                .apis(RequestHandlerSelectors.basePackage("com.hb"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    /*
        构建api文档的详细信息
     */

    public ApiInfo apiInfo(){
        Contact contact = new Contact("zhouhaibo","http://www.zhouhaibo.com","118*******@qq.com");
        return new ApiInfoBuilder()
                .title("hb对接接口适配文档")
                .version("v1.0")
                .contact(contact)
                .description("提供可视化接口文档与测试工具")
                .termsOfServiceUrl("http://www.hb.com/")
                .license("hb版权所有")
                .build();

    }
}
